package com.hucc.api.service;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.hucc.api.domain.entity.Group;
import com.hucc.api.domain.entity.Interface;
import com.hucc.api.domain.entity.InterfaceWithBLOBs;
import com.hucc.api.domain.entity.Project;
import com.hucc.api.domain.enums.InterfaceInTypeEnum;
import com.hucc.api.domain.exceptions.BusinessException;
import com.hucc.api.domain.req.InterfaceReq;
import com.hucc.api.repository.GroupMapper;
import com.hucc.api.repository.InterfaceMapper;
import com.hucc.api.repository.ProjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class InterfaceService {
    @Autowired
    InterfaceMapper interfaceMapper;
    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    GroupMapper groupMapper;

    public void add(InterfaceReq interfaceReq) throws BusinessException {
        if (interfaceReq == null) {
            throw new BusinessException("接口请求不能为空");
        }

        if (StringUtils.isEmpty(interfaceReq.getName())) {
            throw new BusinessException("接口名称不能为空");
        }

        if (StringUtils.isEmpty(interfaceReq.getPath())) {
            throw new BusinessException("接口路径不能为空");
        }

        if (interfaceReq.getGroupId() == null) {
            throw new BusinessException("分组ID不能为空");
        }

        if (interfaceReq.getProjectId() == null) {
            throw new BusinessException("项目ID不能为空");
        }

        if (StringUtils.isEmpty(interfaceReq.getDomain())) {
            throw new BusinessException("接口域名不能为空");
        }

        Project project = projectMapper.selectByPrimaryKey(interfaceReq.getProjectId());
        if (project == null) {
            throw new BusinessException("项目不存在");
        }

        Group group = groupMapper.selectByPrimaryKey(interfaceReq.getGroupId());
        if (group == null) {
            throw new BusinessException("分组不存在");
        }

        List<InterfaceWithBLOBs> interfaceWithBLOBs = interfaceMapper.getByProjectIdAndGroupIdAndPath(interfaceReq.getProjectId(), interfaceReq.getGroupId(), interfaceReq.getPath());
        if (!interfaceWithBLOBs.isEmpty()) {
            throw new BusinessException("接口路径已存在");
        }

        InterfaceWithBLOBs add = new InterfaceWithBLOBs();
        add.setName(interfaceReq.getName());
        add.setProjectId(interfaceReq.getProjectId());
        add.setGroupId(interfaceReq.getGroupId());
        add.setPath(interfaceReq.getPath());
        add.setDomain(interfaceReq.getDomain());
        add.setDesc(interfaceReq.getDesc());
        add.setParameters(interfaceReq.getParameters());
        add.setResult(interfaceReq.getResult());
        add.setCreatetime(new Date());
        add.setUpdatetime(new Date());
        try {
            interfaceMapper.insert(add);
        } catch (Exception e) {
            log.error("添加接口到db失败, interfaceReq: {}", JSON.toJSONString(interfaceReq), e);
            throw new BusinessException("添加接口到db失败");
        }
    }

    public void batchAdd(Long projectId, String version, List<InterfaceWithBLOBs> interfaces) {
        // 低版本version的接口已经存在，怎么处理
        interfaceMapper.deleteLtVersion(projectId, version);

        // 相同版本的接口已经存在怎么处理
        for (InterfaceWithBLOBs interfaceReq : interfaces) {
            InterfaceWithBLOBs interfaceWithBLOBs = interfaceMapper.selectByProjectIdAndVersionAndPath(projectId, version,
                    interfaceReq.getPath(), InterfaceInTypeEnum.AUTO.getType());
            if (interfaceWithBLOBs == null) {
                interfaceMapper.insertSelective(interfaceReq);
            }
        }
    }
}
